

$('#cbbKhachHang').change(function() {
	$("#gridChiTietDonHang").jqGrid().setGridParam(
			{editurl : '/donhang/json/?func=updateChiTietDonHang&maKhachHang=' + $('#cbbKhachHang').val()
				+ '&maDonHang=' + $('#maDonHang').val()});
});

$('#luuDonHang').click(function() {
	$('#loading').show();
	var maKH = $('#cbbKhachHang').val();
	if ($('#duTru').is(':checked') == true)
		maKH = $('#maNV').val();
	$.get('/donhang/json/?func=updateDonHang&maDonHang=' + $('#maDonHang').val()
			+ '&ngayGiao=' + $('#ngayGiao').val()
			+ '&maKhachHang=' + maKH
			+ '&ngayThanhToan=' + $('#ngayThanhToan').val()
			+ '&trangThai=' + $('#cbbTrangThai').val()
			+ '&ghiChu=' + $('#ghiChu').val(), function(data){
		if (data == 1) {
			$('#message').html('Lưu đơn hàng thành công.');
			$('#cbbTrangThai').val('Da dat hang');
			window.location.replace('/donhang/');
		}
		else  
			$('#message').html('Có lỗi xảy ra: ' + data);

		$('#loading').hide();
		});
});

$('#xoaDonHang').click(function() {
	$('#loading').show();
	$.get('/donhang/json/?func=deleteDonHang&maDonHang=' + $('#maDonHang').val(), function(data){
		if (data == 1)
			window.location.replace('/quanlydonhang/');
		else  
			$('#message').html('Có lỗi xảy ra: ' + data);

		$('#loading').hide();
		});
});
var temp;
var tempIdDonHang;
$(document).ready(function() {
	$('#KinhDoanh').addClass('Active');
	$('#maDonHang').attr('disabled', true);
	$('#cbbDonHangMau').attr('disabled', false);
	
	$('#ngayDat').attr('disabled', true);
	$('#ngayGiao').datetimepicker({ dateFormat: "dd/mm/yy" });
	$('#ngayThanhToan').datepicker({ dateFormat: "dd/mm/yy" });
	
	var next = new Date();
	next.setDate(next.getDate() + 1);
	
	
	//$('#ngayGiao').val(next.getDate() + "/" + (next.getMonth()+1) + "/" + next.getFullYear() + " 05:00");
	//$('#ngayThanhToan').val(next.getDate() + "/" + (next.getMonth()+1) + "/" + next.getFullYear());
	$('#duTru').change(function() {
		if ($('#duTru').is(':checked') == true) {
			//get ma don hang
			$('#loading').show();
			$.get('/donhang/json/?func=getIdDonHangDuTru&maNV=' + $('#maNV').val(), function(data){
				maDonHang = data;
				tempIdDonHang = $('#maDonHang').val(); 
				$('#maDonHang').val(maDonHang);
				$('#ngayGiao').attr('disabled', true);
				$('#cbbKhachHang').attr('disabled', true);
				$('#cbbDonHangMau').attr('disabled', true);
				$('#ngayThanhToan').attr('disabled', true);
				$('#gridChiTietDonHang').jqGrid('showCol', 'duTru');
				$('#gridChiTietDonHang').jqGrid('hideCol', 'soLuong');
				//reload grid
				$("#gridChiTietDonHang").jqGrid()
					.setGridParam({url : '/donhang/json/?func=getListChiTietDonHang&maDonHang=' 
						+ maDonHang
					}).trigger('reloadGrid');
				$("#gridChiTietDonHang").jqGrid().setGridParam(
						{editurl : '/donhang/json/?func=updateChiTietDonHang&maKhachHang=' + $('#maNV').val()
							+ '&maDonHang=' + maDonHang});
				$('#loading').hide();
			});
			
		}
		else {
			$('#maDonHang').val(tempIdDonHang);
			$('#cbbKhachHang').attr('disabled', false);
			$('#ngayGiao').attr('disabled', false);
			$('#cbbKhachHang').attr('disabled', false);
			$('#cbbDonHangMau').attr('disabled', false);
			$('#ngayThanhToan').attr('disabled', false);
			$('#gridChiTietDonHang').jqGrid('showCol', 'soLuong');
			$('#gridChiTietDonHang').jqGrid('hideCol', 'duTru');
			
			//reload grid
			$("#gridChiTietDonHang").jqGrid()
				.setGridParam({url : '/donhang/json/?func=getListChiTietDonHang&maDonHang=' 
					+ tempIdDonHang
				}).trigger('reloadGrid');
			$("#gridChiTietDonHang").jqGrid().setGridParam(
					{editurl : '/donhang/json/?func=updateChiTietDonHang&maKhachHang=' + $('#cbbKhachHang').val()
						+ '&maDonHang=' + tempIdDonHang});
		}
	});
	
	
	$('#loading').hide();
	if ($('#priv').val() == 'Owner') {
		$("#gridChiTietDonHang").jqGrid({        
		   	url:'/donhang/json/?func=getListChiTietDonHang&maDonHang=' + $('#maDonHang').val(),
			datatype: "json",
		   	colNames:['id', 'Hàng hóa', 'Số lượng', 'Dự trữ', 'Đơn giá','Thành tiền','Ghi chú', 'Trạng thái'],
		   	colModel:[
		   		{name:'id',index:'maNV', width:55,edittype:"select", editable:false, hidden:true},
		   		{name:'hangHoa',index:'hangHoa', width:190, editable:true, 
		   				edittype:"select", editoptions:{value: $('#lstHangHoa').val()}},
		   		{name:'soLuong',index:'soLuong', width:100, editable:true},
		   		{name:'duTru',index:'duTru', width:100, editable:true},
		   		{name:'donGia',index:'donGia', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'thanhTien',index:'thanhTien', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'ghiChu',index:'ghiChu', width:280, align:"right", editable:true},
		   		{name:'trangThai',index:'trangThai', width:80,align:"right", editable:true, edittype:"select", editoptions:{value: "Duyet:Duyệt; Chua duyet: Chưa duyệt"}},
		   	],
		   	rowNum:10,
		   	rowList:[10,20,30],
		   	pager: '#pChiTietDonHang',
		   	sortname: 'maNV',
		    viewrecords: true,
		    loadonce: false,
		    sortorder: "desc",
			jsonReader: {
				repeatitems : false,
				id: "0"
			},
			afterInsertRow: function(rowid, rowdata, rowelem) {$('#cbbKhachHang').attr('disabled', true);},
			caption: "Chi tiết đơn hàng",
			width: '1071',
		    height: '100%',
			editurl:"/donhang/json/?func=updateChiTietDonHang&maKhachHang=" + $('#cbbKhachHang').val() + '&maDonHang=' + $('#maDonHang').val(),
		});
		
	}
	else if ($('#priv').val() == 'Manager') {
		$("#gridChiTietDonHang").jqGrid({        
		   	url:'/donhang/json/?func=getListChiTietDonHang&maDonHang=' + $('#maDonHang').val(),
			datatype: "json",
		   	colNames:['id', 'Hàng hóa', 'Số lượng', 'Dự trữ', 'Đơn giá','Thành tiền','Ghi chú', 'Trạng thái'],
		   	colModel:[
		   		{name:'id',index:'maNV', width:55,edittype:"select", editable:false, hidden:true},
		   		{name:'hangHoa',index:'hangHoa', width:190, editable:true, 
		   				edittype:"select", editoptions:{value: $('#lstHangHoa').val()}},
		   		{name:'soLuong',index:'soLuong', width:100, editable:true},
		   		{name:'duTru',index:'duTru', width:100, editable:true},
		   		{name:'donGia',index:'donGia', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'thanhTien',index:'thanhTien', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'ghiChu',index:'ghiChu', width:280, align:"right", editable:true},
		   		{name:'trangThai',index:'trangThai', width:80,align:"right", editable:false, edittype:"select", editoptions:{value: "Duyet:Duyệt; Chua duyet: Chưa duyệt"}},
		   	],
		   	rowNum:10,
		   	rowList:[10,20,30],
		   	pager: '#pChiTietDonHang',
		   	sortname: 'maNV',
		    viewrecords: true,
		    loadonce: false,
		    sortorder: "desc",
			jsonReader: {
				repeatitems : false,
				id: "0"
			},
			caption: "Chi tiết đơn hàng",
			width: '1071',
			afterInsertRow: function(rowid, rowdata, rowelem) {$('#cbbKhachHang').attr('disabled', true);},
		    height: '100%',
			editurl:"/donhang/json/?func=updateChiTietDonHang&maKhachHang=" + $('#cbbKhachHang').val() + '&maDonHang=' + $('#maDonHang').val(),
		});
	}
	else if ($('#priv').val() == 'Staff') {
		$("#gridChiTietDonHang").jqGrid({        
		   	url:'/donhang/json/?func=getListChiTietDonHang&maDonHang=' + $('#maDonHang').val(),
			datatype: "json",
		   	colNames:['id', 'Hàng hóa', 'Số lượng', 'Dự trữ', 'Đơn giá','Thành tiền','Ghi chú'],
		   	colModel:[
		   		{name:'id',index:'maNV', width:55,edittype:"select", editable:false, hidden:true},
		   		{name:'hangHoa',index:'hangHoa', width:190, editable:true, 
		   				edittype:"select", editoptions:{value: $('#lstHangHoa').val()}},
		   		{name:'soLuong',index:'soLuong', width:100, editable:true},
		   		{name:'duTru',index:'duTru', width:100, editable:true},
		   		{name:'donGia',index:'donGia', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'thanhTien',index:'thanhTien', width:80, align:"right", formatter:"number", editable:false},
		   		{name:'ghiChu',index:'ghiChu', width:280, align:"right", editable:true}
		   	],
		   	rowNum:10,
		   	rowList:[10,20,30],
		   	pager: '#pChiTietDonHang',
		   	sortname: 'maNV',
		    viewrecords: true,
		    loadonce: false,
		    sortorder: "desc",
			jsonReader: {
				repeatitems : false,
				id: "0"
			},
			caption: "Chi tiết đơn hàng",
			afterInsertRow: function(rowid, rowdata, rowelem) {$('#cbbKhachHang').attr('disabled', true);},
			width: '1071',
		    height: '100%',
			editurl:"/donhang/json/?func=updateChiTietDonHang&maKhachHang=" + $('#cbbKhachHang').val() + '&maDonHang=' + $('#maDonHang').val(),
		});
	}
	


	if ($('#priv').val() == 'Owner') {
		jQuery("#gridChiTietDonHang").jqGrid('navGrid','#pChiTietDonHang',
				{
					edit:true,
					add:true,
					del:true
				});
	}
	else if ($('#priv').val() == 'Staff') {
		if ($('#cbbTrangThai').val() == 'Dang dat hang' || $('#cbbTrangThai').val() == 'Da dat hang') {
			jQuery("#gridChiTietDonHang").jqGrid('navGrid','#pChiTietDonHang',
					{
						edit:true,
						add:true,
						del:true
					});
		}
		else {
			jQuery("#gridChiTietDonHang").jqGrid('navGrid','#pChiTietDonHang',
					{
						edit:false,
						add:false,
						del:false
					});
		}
	}
	else if ($('#priv').val() == 'Manager') {
		jQuery("#gridChiTietDonHang").jqGrid('navGrid','#pChiTietDonHang',
				{
					edit:false,
					add:true,
					del:false
				});
	}
	
	if ($('#cbbTrangThai').val() != 'Da dat hang' && $('#cbbTrangThai').val() != 'Dang dat hang') {
		$('#xoaDonHang').hide();
		$('#ngayGiao').attr('disabled', true);
		$('#ngayThanhToan').attr('disabled', true);
		$('#cbbDonHangMau').attr('disabled', true);
	}

	if ($('#fDuTru').val() == "True") {
		$('#duTru').attr('checked','checked');
		$('#gridChiTietDonHang').jqGrid('hideCol', 'soLuong');
	}
	else {
		$('#gridChiTietDonHang').jqGrid('hideCol', 'duTru');
	}
	
	$('#cbbKhachHang').change(function() {
		//lay danh sach khach hang ve dua vao combobox
		$.get('/donhang/json/?func=getListDonHangMau&maKH=' + $('#cbbKhachHang').val(), function(data){
			var lstKhachHang = '';
			var select = $('#cbbDonHangMau');
			select.find('option').remove();
			var obj = JSON.parse(data);
			var all = document.createElement("option");
			all.value = '';
			all.innerHTML = "Không chọn";
			select.append(all);
			for (var i=0; i < obj.length; i++)
			{
			  var option = document.createElement("option");
			  option.value = obj;
			  option.innerHTML = obj;
			  select.append(option);
			}
		});
	});
	
	$('#cbbDonHangMau').change(function() {
		$('#loading').show();
		$.get('/donhang/json/?func=donHangMau&maDHMau=' + $('#cbbDonHangMau').val() + '&maDH=' + $('#maDonHang').val(), function(data){
			window.location.replace('/donhang/?id=' + $('#maDonHang').val());
		});
	});
});

